package com.wut.clock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.exception.Asserts;
import com.wut.clock.entity.SysRole;
import com.wut.clock.entity.SysRoleMenu;
import com.wut.clock.entity.SysUserRole;
import com.wut.clock.mapper.SysRoleMapper;
import com.wut.clock.mapper.SysRoleMenuMapper;
import com.wut.clock.mapper.SysUserRoleMapper;
import com.wut.clock.service.IRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 角色实现类
 * @date 2022/5/30 15:28
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements IRoleService {

    private final SysRoleMapper roleMapper;

    private final SysUserRoleMapper userRoleMapper;

    private final SysRoleMenuMapper roleMenuMapper;


    @Override
    public SysRole getRoleByUserId(Integer userId) {
        SysUserRole userRole = userRoleMapper.selectOne(new QueryWrapper<SysUserRole>().eq("user_id", userId));
        if(userRole == null) Asserts.fail("该用户没有对应的角色");
        SysRole role = roleMapper.selectById(userRole.getRoleId());
        if(role == null) Asserts.fail("该用户对应的角色不存在");
        return role;
    }

    @Override
    public int insertOneRole(SysRole role) {
        return roleMapper.insert(role);
    }

    @Override
    public int deleteRole(Integer roleId) {
        //清理缓存
        List<SysUserRole> sysUserRoles = roleMapper.listUserIdByRoleId(roleId);
        //删除sys_role_menu中该角色对应的菜单记录
        QueryWrapper<SysRoleMenu> roleMenu = new QueryWrapper<SysRoleMenu>().eq("role_id", roleId);
        if(roleMenu!=null ){
            roleMenuMapper.delete(roleMenu);
        }
        //删除sys_user_role中的记录
        QueryWrapper<SysUserRole> userRole = new QueryWrapper<SysUserRole>().eq("role_id", roleId);
        if(userRole!=null ){
            userRoleMapper.delete(userRole);
        }
        //删除该角色
        return roleMapper.deleteById(roleId);
    }

    @Override
    public int updateRole(SysRole role) {
        return roleMapper.updateById(role);
    }

    @Override
    public CommonPage<SysRole> getRoleList(Integer pageNum, Integer pageSize, String name, String nickName) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if(name != null) wrapper.eq("name",name);
        if(nickName != null) wrapper.eq("nick_name",nickName);
        wrapper.orderByDesc("create_time");
        List<SysRole> roleList = roleMapper.selectList(wrapper);
        return CommonPage.restPage(roleList);
    }

    @Transactional
    @Override
    public int assignMenuRole(Integer roleId, String menuIdString) {
        clearMenu2Role(roleId);
        if(StringUtils.isNotBlank(menuIdString)){
            String[] menuString = menuIdString.split(",");
            int[] menuIds = Arrays.stream(menuString).mapToInt(Integer::parseInt).distinct().toArray();
            Arrays.stream(menuIds).forEach(id->{
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(id);
                roleMenuMapper.insert(roleMenu);
            });
            return 1;
        }
        return -1;
    }

    private void clearMenu2Role(Integer roleId){
        QueryWrapper<SysRoleMenu> roleMenu = new QueryWrapper<SysRoleMenu>().eq("role_id", roleId);
        if(roleMenu!=null){
            roleMenuMapper.delete(roleMenu);
        }
    }
}
